package com.rybbaby.tss.front.tssMemberCheckAttendance.service.imp;

import com.rybbaby.tss.core.utils.SequenceUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssClassAttendance.service.ITssClassAttendanceService;
import com.rybbaby.tss.front.tssClassesLessons.dao.ITssClassesLessonsDao;
import com.rybbaby.tss.front.tssClassesLessons.entity.TssClassesLessons;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.dao.ITssMemberAttendClassRecordDao;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.entity.TssMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.service.ITssMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.dao.ITssMemberCheckAttendanceDao;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.TssMemberCheckAttendance;
import com.rybbaby.tss.front.tssMemberCheckAttendance.service.ITssMemberCheckAttendanceService;
import com.rybbaby.tss.front.tssMemberPackage.dao.ITssMemberPackageDao;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description 会员考勤记录表数据库操作服务实现
 * 
 * @author 雷春琼
 *
 * @createDate 2017-08-10 上午10:33:46
 */
@Service
@Transactional
public class TssMemberCheckAttendanceServiceImp extends BaseServiceImp<TssMemberCheckAttendance> implements ITssMemberCheckAttendanceService {

    @Autowired
    private ITssMemberCheckAttendanceDao tssMemberCheckAttendanceDao;
    @Autowired
    private ITssMemberAttendClassRecordService tssMemberAttendClassRecordService;
    @Autowired
    private ITssClassAttendanceService tssClassAttendanceService;
    @Autowired
    private ITssMemberPackageDao tssMemberPackageDao;
    @Autowired
    private ITssClassesLessonsDao tssClassesLessonsDao;
    @Autowired
    private ITssMemberAttendClassRecordDao tssMemberAttendClassRecordDao;


    @Override
    public String test(String abd) {
        System.out.println("-----------------------测试"+new Date()+"------------------------");
        return  abd;
    }

    @Override
    public int batchAttendance(List<String> ids, String empId) {
        String paramId = "'";
        for (int j = 0; j < ids.size(); j++) {
            if(j == (ids.size()-1)){
                paramId = paramId + ids.get(j) + "'";
            }else {
                paramId = paramId + ids.get(j) + "','";
            }
        }
        //System.out.println("paramId:"+paramId);
        return tssMemberCheckAttendanceDao.batchAttendance(paramId,empId);
    }

    @Override
    public List<TssMemberCheckAttendance> attendanceNumber(String lessonsId,int attendenceStatus) throws Exception{
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return tssMemberCheckAttendanceDao.attendanceNumber(lessonsId,attendenceStatus);
    }

    @Override
    public List<TssMemberCheckAttendance> sumNumber(String lessonsId) throws Exception {
        /*SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(memberCheckAttendanceCreateTime);*/
        return tssMemberCheckAttendanceDao.sumNumber(lessonsId);
    }

    @Override
    public void updateClassAttendance(String lessonsId) throws Exception{
        //查询未考勤人数
        List<TssMemberCheckAttendance> sumAttendance = this.attendanceNumber(lessonsId,0);
        //查询总人数
        List<TssMemberCheckAttendance> sumPeople = this.sumNumber(lessonsId);
        int classAttendance = 1;
        if(sumAttendance.size() < sumPeople.size()){
            classAttendance = 2;
        }
        if(sumAttendance.size() == 0){
            classAttendance = 3;
        }
        if(sumAttendance.size() == sumPeople.size()){
            classAttendance = 1;
        }
        //调用 班级考勤 service更新 班级考勤状态
        int i = tssClassAttendanceService.updateClassAttendance(lessonsId, classAttendance);
        //System.out.println("11111111111111111111111111111111111"+i);
    }

    @Override
    public List<String> notAttendanceIds(List<String> lessonsIds) throws Exception{
        ArrayList<String> strings = new ArrayList<>();
        for (int j = 0; j < lessonsIds.size(); j++) {
            List<TssMemberCheckAttendance> memberCheckAttendancesList = tssMemberCheckAttendanceDao.notAttendanceIds(lessonsIds.get(j));
            for (int i = 0; i < memberCheckAttendancesList.size(); i++) {
                strings.add(i,memberCheckAttendancesList.get(i).getId());
                //System.out.println(strings);
            }
        }
        return strings;
    }

    @Override
    public int attendance(Integer attendenceStatus, String empId, String id) {
        return tssMemberCheckAttendanceDao.attendance(attendenceStatus,empId,id);
    }

    @Override
    public int Remarks(String remark, String empId, String id) {
        return tssMemberCheckAttendanceDao.Remarks(remark,empId,id);
    }

    @Override
    public List<TssMemberCheckAttendance> findHistory(List<String> ids) throws Exception {
        String paramId = "'";
        for (int j = 0; j < ids.size(); j++) {
            if(j == (ids.size()-1)){
                paramId = paramId + ids.get(j) + "'";
            }else {
                paramId = paramId + ids.get(j) + "','";
            }
        }
        return tssMemberCheckAttendanceDao.findHistory(paramId);
    }

    @Override
    public boolean saveEntity(String targetDataId, String memberPackageId) throws Exception {
        String ss[] = StringUtils.split(targetDataId, ",");
        TssMemberPackage memberPackage = tssMemberPackageDao.get(TssMemberPackage.class, memberPackageId);
        List<TssMemberCheckAttendance> tssMemberCheckAttendanceList = new ArrayList<>();
        TssMemberCheckAttendance entity = new TssMemberCheckAttendance();
        for (String s1 : ss) {
            TssClassesLessons tssClassesLessons = tssClassesLessonsDao.get(TssClassesLessons.class, s1);
            /*Map<String,Object> map = tssMemberAttendClassRecordDao.findByLessonsId(s1,tssClassesLessons.getClassTime(),memberPackage.getMemberId());
            TssMemberAttendClassRecord tssMemberAttendClassRecord = new TssMemberAttendClassRecord();
            if(map.get("id") != null){
                entity.setAttendClassId(map.get("id").toString());
            }*/
            List<TssMemberAttendClassRecord> tssMemberAttendClassRecords = tssMemberAttendClassRecordService.findAttendClassRecordByLessonsIdMemberId(memberPackage.getMemberId(),tssClassesLessons.getId());
            entity = new TssMemberCheckAttendance();
            entity.setId(SequenceUtil.getUpperCaseUUID());
            entity.setMemberId(memberPackage.getMemberId());
            entity.setClassId(tssClassesLessons.getClassId());
            entity.setAttendClassId(tssMemberAttendClassRecords.get(0).getId());
            entity.setType(memberPackage.getType());
            entity.setAttendenceStatus(0);
            /*entity.setFormalClass(tssClassesLessons.getFormalClass());
            entity.setGiftClass(tssClassesLessons.getGiftClass());*/
            entity.setCreateTime(tssClassesLessons.getClassTime());
            entity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            tssMemberCheckAttendanceList.add(entity);
        }
        //批量保存
        boolean bool = tssMemberCheckAttendanceDao.jdbcBatchSave(tssMemberCheckAttendanceList);

        /*List<TssMemberCheckAttendance> all = tssMemberCheckAttendanceDao.loadAll(TssMemberCheckAttendance.class);
        for (TssMemberCheckAttendance a:all) {

        }*/

        return bool;
    }

    @Override
    public List<Map<String, Object>> findTodayAttendanceStatisticsByType(String adminUserIds) {
        return tssMemberCheckAttendanceDao.findTodayAttendanceStatisticsByType(adminUserIds);
    }

    @Override
    public List<Map<String, Object>> findClassAttendanceByType(String adminUserIds) {
        return tssMemberCheckAttendanceDao.findClassAttendanceByType(adminUserIds);
    }

    @Override
    public Map<String, Object> findPackageInformation(String id) {
        return tssMemberCheckAttendanceDao.findPackageInformation(id);
    }

    @Override
    public List<Map<String, Object>> findPackageInformationList(List<String> ids) {
        String paramId = "'";
        for (int j = 0; j < ids.size(); j++) {
            if(j == (ids.size()-1)){
                paramId = paramId + ids.get(j) + "'";
            }else {
                paramId = paramId + ids.get(j) + "','";
            }
        }
        return tssMemberCheckAttendanceDao.findPackageInformationList(paramId);
    }

    @Override
    public Map<String, Object> findAttendanceMaintenance(String attendanceId) {
        return tssMemberCheckAttendanceDao.findAttendanceMaintenance(attendanceId);
    }

    @Override
    public int updateCheckAttendance(String empId, String remark, Integer attendenceStatus,String attendanceId) {
        return tssMemberCheckAttendanceDao.updateCheckAttendance(empId,remark,attendenceStatus,attendanceId);
    }

    @Override
    public Map<String, Object> findAttendanceMaintenancePackage(String attendanceId) {
        return tssMemberCheckAttendanceDao.findAttendanceMaintenancePackage(attendanceId);
    }

    @Override
    public Map<String, Object> attendanceStatistics(String lessonsId) {
        return tssMemberCheckAttendanceDao.attendanceStatistics(lessonsId);
    }
}
